package edu.njit.hz278.controller.staff;

import java.util.Collections;
import java.util.List;

import edu.njit.hz278.bean.Employee;
import edu.njit.hz278.bean.Nurse;
import edu.njit.hz278.bean.Physician;
import edu.njit.hz278.bean.Surgeon;
import edu.njit.hz278.controller.Constants;
import edu.njit.hz278.core.db.IQuery;
import edu.njit.hz278.core.db.Query;
import edu.njit.hz278.core.web.Get;
import edu.njit.hz278.core.web.Param;

public final class View {

	private static final IQuery<Employee> queryForStaff = new Query<>(
			Employee.class);
	private static final IQuery<Nurse> queryForNurse = new Query<>(Nurse.class);
	private static final IQuery<Physician> queryForPhysician = new Query<>(
			Physician.class);
	private static final IQuery<Surgeon> queryForSurgeon = new Query<>(
			Surgeon.class);

	@Get
	public List<Employee> all(@Param("page") int page) {
		return queryForStaff.list(page, Constants.DEFAULT_PAGE_SIZE);
	}

	@Get
	public List<Employee> staffs(@Param("page") int page) {
		return queryForStaff.get("empType", Employee.TYPE_STAFF, page,
				Constants.DEFAULT_PAGE_SIZE);
	}

	@Get
	public List<Nurse> nurses(@Param("page") int page) {

		if (page < 1)
			return Collections.emptyList();

		return queryForNurse
				.getBySQL("SELECT * FROM employee JOIN nurseinfo ON employee.empid = nurseinfo.empid LIMIT ?, ?",
						(page - 1) * Constants.DEFAULT_PAGE_SIZE,
						Constants.DEFAULT_PAGE_SIZE);
	}

	@Get
	public List<Physician> physicians(@Param("page") int page) {
		if (page < 1)
			return Collections.emptyList();

		return queryForPhysician
				.getBySQL("SELECT * FROM employee JOIN physicianinfo ON employee.empid = physicianinfo.empid LIMIT ?, ?",
						(page - 1) * Constants.DEFAULT_PAGE_SIZE,
						Constants.DEFAULT_PAGE_SIZE);
	}

	@Get
	public List<Surgeon> surgeons(@Param("page") int page) {
		if (page < 1)
			return Collections.emptyList();

		return queryForSurgeon
				.getBySQL("SELECT * FROM employee, physicianinfo, surgeoninfo WHERE employee.empid = physicianinfo.empid AND physicianinfo.empid = surgeoninfo.empid LIMIT ?, ?",
						(page - 1) * Constants.DEFAULT_PAGE_SIZE,
						Constants.DEFAULT_PAGE_SIZE);
	}
}
